//
//  XYLLocationAnnotationView.m
//  XYLBaiduMap2017
//
//  Created by 夏玉林 on 17/2/16.
//  Copyright © 2017年 夏玉林. All rights reserved.
//

#import "XYLLocationAnnotationView.h"
#import "UIView+Extension.h"
#import "XYLPaoPaoView.h"

// 16进制转
#define UIColorFromRGBWithAlpha(rgbValue,alphaValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:alphaValue]

@interface XYLLocationAnnotationView ()



//!背景图片View
@property (nonatomic, strong) UIImageView *backImageView;

@property (nonatomic, strong) CALayer *colorDotLayer;

@property (nonatomic, strong) CALayer *outerDotLayer;

//字定义泡泡
@property (nonatomic, strong)XYLPaoPaoView *customPaoPaoView;

@end

@implementation XYLLocationAnnotationView

#pragma mark - =====================懒加载====================
- (XYLPaoPaoView *)customPaoPaoView
{
    if (!_customPaoPaoView)
    {
        _customPaoPaoView = [[XYLPaoPaoView alloc] init];
    }
    return _customPaoPaoView;
}
- (void)setBackImage:(UIImage *)backImage
{
    _backImage = backImage;
    
    self.backImageView.image = backImage;
}
- (UIImageView *)backImageView
{
    if (!_backImageView)
    {
        _backImageView = [[UIImageView alloc] initWithFrame:self.bounds];
    }
    return _backImageView;
}

- (UIImageView *)headerImageView
{
    if (!_headerImageView)
    {
        _headerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(3, 2.5, 36, 36)];
        
        [_headerImageView setBoundWidth:0 cornerRadius:36/2.0];
        //_headerImageView.image = [UIImage imageNamed:@"180*180"];
    }
    return _headerImageView;
}
- (UIButton *)clickBtnAnnotaionView{
    if (!_clickBtnAnnotaionView) {
        _clickBtnAnnotaionView = [UIButton buttonWithType:UIButtonTypeCustom];
        _clickBtnAnnotaionView.backgroundColor = [UIColor clearColor];
        _clickBtnAnnotaionView.frame = CGRectMake(0, 0, 50 , 50);
        [_clickBtnAnnotaionView setImage:[UIImage imageNamed:@"lbs_weidianji"] forState:UIControlStateNormal];
        [_clickBtnAnnotaionView setImage:[UIImage imageNamed:@"lbs_xuanzhong"] forState:UIControlStateSelected];
        [_clickBtnAnnotaionView addTarget:self action:@selector(clickForButton:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _clickBtnAnnotaionView;
}

//!外圈
- (CALayer*)outerDotLayer {
    if(!_outerDotLayer) {
        _outerDotLayer = [CALayer layer];
        _outerDotLayer.bounds = self.bounds;
        _outerDotLayer.contents = (id)[self circleImageWithColor:self.outerColor height:self.bounds.size.height].CGImage;
        _outerDotLayer.position = CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2);
        _outerDotLayer.contentsGravity = kCAGravityCenter;
        _outerDotLayer.contentsScale = [UIScreen mainScreen].scale;
        _outerDotLayer.shadowColor = [UIColor blackColor].CGColor;
        _outerDotLayer.shadowOffset = CGSizeMake(0, 2);
        _outerDotLayer.shadowRadius = 3;
        _outerDotLayer.shadowOpacity = 0.3;
        _outerDotLayer.shouldRasterize = YES;
        _outerDotLayer.rasterizationScale = [UIScreen mainScreen].scale;
    }
    return _outerDotLayer;
}
//!内圈
- (CALayer*)colorDotLayer
{
    if(!_colorDotLayer)
    {
        _colorDotLayer = [CALayer layer];
        CGFloat width = self.bounds.size.width-2;
        
        _colorDotLayer.bounds = CGRectMake(0, 0, width, width);
        _colorDotLayer.allowsGroupOpacity = YES;
        _colorDotLayer.backgroundColor = self.annotationColor.CGColor;
        _colorDotLayer.cornerRadius = width/2;
        _colorDotLayer.position = CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2);

        // 去掉动画
#if 0
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
            
            if(self.delayBetweenPulseCycles != INFINITY) {
                CAMediaTimingFunction *defaultCurve = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault];
                
                CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
                animationGroup.duration = self.pulseAnimationDuration;
                animationGroup.repeatCount = INFINITY;
                animationGroup.removedOnCompletion = NO;
                animationGroup.autoreverses = YES;
                animationGroup.timingFunction = defaultCurve;
                animationGroup.speed = 1;
                animationGroup.fillMode = kCAFillModeBoth;
                
                CABasicAnimation *pulseAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale.xy"];
                pulseAnimation.fromValue = @0.8;
                pulseAnimation.toValue = @1;
                pulseAnimation.duration = self.pulseAnimationDuration;
                
                CABasicAnimation *opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
                opacityAnimation.fromValue = @0.8;
                opacityAnimation.toValue = @1;
                opacityAnimation.duration = self.pulseAnimationDuration;
                
                animationGroup.animations = @[pulseAnimation, opacityAnimation];
                
                dispatch_async(dispatch_get_main_queue(), ^(void) {
                    [_colorDotLayer addAnimation:animationGroup forKey:@"pulse"];
                });
            }
        });
#endif
    }
    return _colorDotLayer;
}

#pragma mark - =====================UIView方法====================
- (id)initWithAnnotation:(id<BMKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
    
    if (self)
    {
        
#pragma mark============在这里修改定位点的 frame===============
        self.bounds = CGRectMake(0, 0, 50 , 50);
#pragma mark=========开发中有时要修改定位点注意这里是像素而不是尺寸==================
        //中点向下移
        self.centerOffset = CGPointMake(0,-50);
        
        self.pulseAnimationDuration = 1;
        
        self.delayBetweenPulseCycles = 0;
        
        //颜色
       // self.annotationColor = [UIColor colorWithRed:2/255.0 green:205/255.0 blue:218/255.0 alpha:1];
        self.annotationColor = [UIColor clearColor];
      //  self.outerColor = [UIColor whiteColor];
    }
    return self;
}

- (void)willMoveToSuperview:(UIView *)newSuperview
{
    if (newSuperview)
        [self reloadLayers];
}
//这个是显示的图层
#pragma mark=========================
- (void)reloadLayers
{

    [_outerDotLayer removeFromSuperlayer];
    
    _outerDotLayer = nil;
    
    [self.layer addSublayer:self.outerDotLayer];
    
    [_colorDotLayer removeFromSuperlayer];
    
    _colorDotLayer = nil;
    
    [self.layer addSublayer:self.colorDotLayer];
    
    if (self.clickBtnAnnotaionView) {
        [self addSubview:self.clickBtnAnnotaionView];
    }
    
    if (self.backImage)
        [self addSubview:self.backImageView];
    
    if (self.headerImageView)
        [self addSubview:self.headerImageView];
}

- (UIImage*)circleImageWithColor:(UIColor*)color height:(float)height {
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(height, height), NO, 0);
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    
    UIBezierPath* fillPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, height, height)];
    [color setFill];
    [fillPath fill];
    
    UIImage *dotImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    CGColorSpaceRelease(colorSpace);
    
    return dotImage;
}

//定位的泡泡
// 在这里进行自定义的用户显示操作
- (UIView *)setupItemAnnotationIsCanViewtitle:(NSString *)title subtitle:(NSString *)subtitle{
    
   // CGRect customFrame;
    
    self.customPaoPaoView.iconImageView.frame = self.bounds;
    
    return self.customPaoPaoView;
}

- (void)clickForButton:(UIButton *)sender{
    NSLog(@"选中下标%ld",sender.tag);
    self.block(sender);
}

@end
